package com.cyms.modules.order.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cyms.common.utils.PageUtils;
import com.cyms.common.utils.Query;
import com.cyms.modules.base.entity.CarEntity;
import com.cyms.modules.base.service.CarService;
import com.cyms.modules.order.dao.CarPayDao;
import com.cyms.modules.order.entity.CarPayEntity;
import com.cyms.modules.order.service.CarPayService;


@Service("carPayService")
public class CarPayServiceImpl extends ServiceImpl<CarPayDao, CarPayEntity> implements CarPayService {
	@Autowired
    private CarService carService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Long createOrg = (Long) params.get("createOrg");
        String code = (String) params.get("code");
        String startDate = (String) params.get("startDate");
        String endDate = (String) params.get("endDate");
        List<CarEntity> carList = carService.selectList(new EntityWrapper<CarEntity>()
                .eq("create_org",createOrg)
                .like(StringUtils.isNotBlank(code),"code",code)
                );
        System.out.println(carList.size() == 0 );
        if (carList.size() == 0 ) {
            return  new PageUtils(new Page<>());
        }
        Page<CarPayEntity> page = this.selectPage(
                new Query<CarPayEntity>(params).getPage(),
                new EntityWrapper<CarPayEntity>()
                        .in("car_id", carList.parallelStream().map(c->c.getId()).collect(Collectors.toList()))
                        .ge(StringUtils.isNotBlank(startDate),"pay_date",startDate)
                        .le(StringUtils.isNotBlank(endDate),"pay_date",endDate)
        );
        page.getRecords().parallelStream().forEach(carPay ->{
        	CarEntity car = carService.selectById(carPay.getCarId());
        	carPay.setCar(car);
        });
        return new PageUtils(page);
    }

}
